package etc.server.csp;

import cn.hutool.core.util.StrUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CspServerHandler extends ChannelInboundHandlerAdapter {

    private final String ENCODING = "GBK";

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("client connected: {}", ctx.channel().remoteAddress());
        super.channelActive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        log.info("client==C==receive==forward====================================================");
        // 在这里处理从客户端接收到的消息
        String req = (String) msg;
        if (StrUtil.isNotEmpty(req)) {
            log.info("C--server--receive--req:{}", req);
        } else {
            log.error("request is null");
        }
        ctx.writeAndFlush(req);
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("C disconnected", cause);
        ctx.close();
    }
}